package net.seabook.games.tetris.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.seabook.games.tetris.model.SettingSystem;
import net.seabook.games.tetris.model.TetrisBoard;
import net.seabook.games.tetris.model.Tile;

public class ControlPanel extends JPanel implements ItemListener,
		ActionListener {
	private static final long serialVersionUID = 760576280723049442L;

	private static final int WIDTH = 3 * TetrisBoard.BOARD_WIDTH / 5;
	private static final int HEIGHT = 3 * TetrisBoard.BOARD_HEIGHT / 5;

	private static final Dimension labelDimension = new Dimension(
			TetrisBoard.BOARD_WIDTH / 5, 2 * Tile.HEIGHT);

	private JLabel level;
	private JLabel levelTxt;
	private JLabel score;
	private JLabel scoreTxt;
	private JLabel line;
	private JLabel lineTxt;

	private JCheckBox showGrid;
	private JButton pauseOrResume;

	private SettingSystem settingSystem;

	public ControlPanel(SettingSystem scoreSystem) {
		JPanel infoPanel = new JPanel(new GridLayout(0, 2));
		this.settingSystem = scoreSystem;

		level = new JLabel("Level:", JLabel.LEFT);
		level.setPreferredSize(labelDimension);
		levelTxt = new JLabel(scoreSystem.wrapperLevel(), JLabel.LEFT);
		levelTxt.setPreferredSize(labelDimension);
		scoreSystem.addPropertyChangeListener(pclLevel);

		score = new JLabel("Score:", JLabel.LEFT);
		score.setPreferredSize(labelDimension);
		scoreTxt = new JLabel(scoreSystem.wrapperTotalScore(), JLabel.LEFT);
		scoreTxt.setPreferredSize(labelDimension);
		scoreSystem.addPropertyChangeListener(pclScore);

		line = new JLabel("Line:", JLabel.LEFT);
		line.setPreferredSize(labelDimension);
		lineTxt = new JLabel(scoreSystem.wrapperLines(), JLabel.LEFT);
		lineTxt.setPreferredSize(labelDimension);
		scoreSystem.addPropertyChangeListener(pclLines);

		infoPanel.add(level);
		infoPanel.add(levelTxt);
		infoPanel.add(score);
		infoPanel.add(scoreTxt);
		infoPanel.add(line);
		infoPanel.add(lineTxt);

		JPanel settingPanel = new JPanel(new GridLayout(0, 1));

		showGrid = new JCheckBox("showGrid?", true);
		showGrid.setAlignmentX(CENTER_ALIGNMENT);
		showGrid.addItemListener(this);

		pauseOrResume = new JButton("Pause");
		pauseOrResume.setAlignmentX(CENTER_ALIGNMENT);
		pauseOrResume.addActionListener(this);

		settingPanel.add(showGrid);
		settingPanel.add(pauseOrResume);

		this.add(infoPanel);
		this.add(settingPanel);
	}

	public void itemStateChanged(ItemEvent e) {
		Object source = e.getItemSelectable();
		if (source == showGrid) {
			if (e.getStateChange() == ItemEvent.DESELECTED) {
				settingSystem.setDrawGrid(false);
			} else {
				settingSystem.setDrawGrid(true);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if ("Pause".equalsIgnoreCase(e.getActionCommand())) {
			settingSystem.setStopped(true);
			pauseOrResume.setText("Resume");
		} else {
			settingSystem.setStopped(false);
			pauseOrResume.setText("Pause");
		}

	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(WIDTH, HEIGHT);
	}

	PropertyChangeListener pclLines = new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
			String property = propertyChangeEvent.getPropertyName();
			if ("lines".equals(property)) {
				lineTxt.setText(String.valueOf(propertyChangeEvent
						.getNewValue()));
			}
		}
	};

	PropertyChangeListener pclScore = new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
			String property = propertyChangeEvent.getPropertyName();
			if ("totalScore".equals(property)) {
				scoreTxt.setText(String.valueOf(propertyChangeEvent
						.getNewValue()));
			}
		}
	};

	PropertyChangeListener pclLevel = new PropertyChangeListener() {
		public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
			String property = propertyChangeEvent.getPropertyName();
			if ("level".equals(property)) {
				levelTxt.setText(String.valueOf(propertyChangeEvent
						.getNewValue()));
			}
		}
	};

	public SettingSystem getScoreSystem() {
		return settingSystem;
	}

	public void setScoreSystem(SettingSystem scoreSystem) {
		this.settingSystem = scoreSystem;
	}

}
